package com.freesky.quartz.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.freesky.biz.activity.domain.TRpaTask;
import com.freesky.biz.activity.mapper.RpaTaskMapper;
import com.freesky.biz.citic.mapper.CiticNewMapper;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生成RPA采集任务表
 */
@Component("rpaGeneralTask")
public class RpaGeneralTask {

    private static final Logger logger = LoggerFactory.getLogger(RpaGeneralTask.class);

    @Autowired
    private RpaTaskMapper rpaTaskMapper;

    public void taskProcess() {
        logger.info("RpaGeneralTask task start");

        List<TRpaTask> generalTaskList = new ArrayList<>();

        String taskTime = DateUtil.format(new Date(),"yyyy-MM-dd")+" 12:01:00";

        Date yesterday = DateUtil.offsetDay(new Date(), -1);

        //取昨天的数据
        List<TRpaTask> rpaTaskList = rpaTaskMapper.selectGeneralTaskList(DateUtil.format(yesterday,"yyyy-MM-dd"));
        for(TRpaTask rpaTask : rpaTaskList){
            if("0".equalsIgnoreCase(rpaTask.getProcessed())
                    || "1".equalsIgnoreCase(rpaTask.getProcessed())){
                continue;
            }

            String startTime = rpaTask.getStartTime();
            if(DateUtil.parse(startTime,"yyyy-MM-dd").isAfter(yesterday)){
                continue;
            }

            if(StringUtils.isNotEmpty(rpaTask.getLastEndTIme())){
                //最后一次的时间
                Date lastEndTime = DateUtil.parse(rpaTask.getLastEndTIme(),"yyyy-MM-dd");
                if(DateUtil.isSameDay(yesterday,lastEndTime)){
                    continue;
                }

                if(lastEndTime.after(yesterday)){
                    continue;
                }
                Date curEndTime = DateUtil.offsetDay(lastEndTime,1);
                rpaTask.setStartTime(DateUtil.format(curEndTime,"yyyy-MM-dd") +" 00:00:00");
            } else {
                rpaTask.setStartTime(startTime);
            }

            String endTime = DateUtil.format(yesterday,"yyyy-MM-dd") +" 23:59:59";
            long dif = DateUtil.between(DateUtil.parse(rpaTask.getStartTime(),"yyyy-MM-dd"),
                    DateUtil.parse(endTime,"yyyy-MM-dd"), DateUnit.DAY,false);
            if(dif>=31){
                //大于31天时候，取30天的数据
                Date curEndTime = DateUtil.offsetDay((DateUtil.parse(rpaTask.getStartTime(),"yyyy-MM-dd")),30);
                endTime = DateUtil.format(curEndTime,"yyyy-MM-dd") +" 23:59:59";
            }
            rpaTask.setEndTime(endTime);

            rpaTask.setId(SeqUtil.getSeqId());
            rpaTask.setProcessed("0");
            rpaTask.setTaskTime(taskTime);
            generalTaskList.add(rpaTask);
        }
        if(generalTaskList.size()>0){
            rpaTaskMapper.addTaskList(generalTaskList);
        }
    }

}
